// get record in  mongodb and rds db.
// first to get in mongodb.

package getrecord

import (
	conf "config"
	"errors"
	"github.com/extrame/goblet"
	"log"
	"time"
)

type GetinfoRecords struct {
	// Id             int64
	Key            string
	IdCode         string
	UserId         int64
	EnterpriseUuid string
	EnterpriseName string
	ReqCodes       string
	GetinfoDate    time.Time
}

func Getrecord(idcode, key string) (*GetinfoRecords, error) {
	record := new(GetinfoRecords)
	if conf.Config.UseMongodb {
		return record, nil
	}
	exist, err := goblet.DB.Where("`key`= ?", key).Get(record)
	if exist {
		return record, nil
	} else if err != nil {
		log.Println("getrecord_err:", err, record)
		return nil, err
	}
	return nil, errors.New("not_found_record")
}

func Putrecord(record *GetinfoRecords) error {
	// putrecord into mongoDB.

	if exist, _ := goblet.DB.Where("id_code= ? and `key`= ?", record.IdCode, record.Key).Get(new(GetinfoRecords)); exist {
		return errors.New("error_repeated")
	}
	_, err := goblet.DB.Insert(record)
	return err
}
